package org.csu.ssm_backend.service.util;

import org.springframework.stereotype.Repository;

import java.security.MessageDigest;

@Repository
public class MD5Util {
    /**
     * MD5加密
     * */
    public static String transformToMD5(String str){
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e){
            e.printStackTrace();
            return "";
        }
        char[] charArray = str.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++){
            byteArray[i] = (byte) charArray[i];
        }
        byte[] md5ByteArray = md5.digest(byteArray);
        StringBuilder buffer = new StringBuilder();
        for (byte b : md5ByteArray) {
            int val = ((int) b) & 0xff;
            if (val < 16) {
                buffer.append("0");
            }
            buffer.append(Integer.toHexString(val));
        }
        return buffer.toString();
    }

    /**
     * MD5加密解密 一次加密二次解密
     * */
    public static String convertMD5(String str){
        char[] a = str.toCharArray();
        for (int i = 0; i < a.length; i++){
            a[i] = (char) (a[i] ^ 't');
        }
        return new String(a);
    }
}
